package l1j.opqlo.Server.etcitem;

import java.lang.reflect.Constructor;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.IdFactory;
import l1j.server.server.datatables.FurnitureSpawnTable;
import l1j.server.server.datatables.NpcTable;
import l1j.server.server.model.L1HouseLocation;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.Instance.L1FurnitureInstance;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.templates.L1Npc;

public class Furniture {

	/** 家具類別 */

	private final static Logger _log = Logger.getLogger(Furniture.class
			.getName());

	public static void begin(int[] newData, final L1PcInstance pc,
			final L1ItemInstance l1iteminstance) {
		// } else if (itemId >= 41383 && itemId <= 41400) { // 傢俱

		final int itemId = l1iteminstance.getItemId();
		final int itemObjid = newData[0];

		if (!L1HouseLocation.isInHouse(pc.getX(), pc.getY(), pc.getMapId())) {
			pc.sendPackets(new S_ServerMessage(563)); // \f1ここでは使えません。
			return;
		}

		boolean isAppear = true;
		L1FurnitureInstance furniture = null;
		for (final L1Object l1object : L1World.getInstance().getObject()) {
			if (l1object instanceof L1FurnitureInstance) {
				furniture = (L1FurnitureInstance) l1object;
				if (furniture.getItemObjId() == itemObjid) { // 既に引き出している傢俱
					isAppear = false;
					break;
				}
			}
		}

		if (isAppear) {
			if (pc.getHeading() != 0 && pc.getHeading() != 2) {
				return;
			}
			int npcId = 0;
			if (itemId == 41383) { // ジャイアントアントソルジャーの剝制
				npcId = 80109;
			} else if (itemId == 41384) { // ベアーの剝制
				npcId = 80110;
			} else if (itemId == 41385) { // ラミアの剝制
				npcId = 80113;
			} else if (itemId == 41386) { // ブラックタイガーの剝制
				npcId = 80114;
			} else if (itemId == 41387) { // 鹿の剝制
				npcId = 80115;
			} else if (itemId == 41388) { // ハーピーの剝制
				npcId = 80124;
			} else if (itemId == 41389) { // ブロンズナイト
				npcId = 80118;
			} else if (itemId == 41390) { // ブロンズホース
				npcId = 80119;
			} else if (itemId == 41391) { // 燭台
				npcId = 80120;
			} else if (itemId == 41392) { // ティーテーブル
				npcId = 80121;
			} else if (itemId == 41393) { // 火缽
				npcId = 80126;
			} else if (itemId == 41394) { // たいまつ
				npcId = 80125;
			} else if (itemId == 41395) { // 君主用のお立ち台
				npcId = 80111;
			} else if (itemId == 41396) { // 旗
				npcId = 80112;
			} else if (itemId == 41397) { // ティーテーブル用の椅子(右)
				npcId = 80116;
			} else if (itemId == 41398) { // ティーテーブル用の椅子(左)
				npcId = 80117;
			} else if (itemId == 41399) { // パーティション(右)
				npcId = 80122;
			} else if (itemId == 41400) { // パーティション(左)
				npcId = 80123;
			}

			try {
				final L1Npc l1npc = NpcTable.getInstance().getTemplate(npcId);
				if (l1npc != null) {
					// Object obj = null;
					try {
						final String s = l1npc.getImpl();
						final Constructor<?> constructor = Class.forName(
								"l1j.server.server.model.Instance." + s
										+ "Instance").getConstructors()[0];
						final Object aobj[] = { l1npc };
						furniture = (L1FurnitureInstance) constructor
								.newInstance(aobj);

						furniture.setId(IdFactory.getInstance().nextId());
						furniture.setMap(pc.getMapId());
						if (pc.getHeading() == 0) {
							furniture.setX(pc.getX());
							furniture.setY(pc.getY() - 1);
						} else if (pc.getHeading() == 2) {
							furniture.setX(pc.getX() + 1);
							furniture.setY(pc.getY());
						}
						furniture.setHomeX(furniture.getX());
						furniture.setHomeY(furniture.getY());
						furniture.setHeading(0);
						furniture.setItemObjId(itemObjid);

						L1World.getInstance().storeObject(furniture);
						L1World.getInstance().addVisibleObject(furniture);
						FurnitureSpawnTable.getInstance().insertFurniture(
								furniture);
					} catch (final Exception e) {
						_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
					}
				}
			} catch (final Exception exception) {
			}
		} else {
			furniture.deleteMe();
			FurnitureSpawnTable.getInstance().deleteFurniture(furniture);
		}
	}
}
